﻿using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils.Extensions;
using EyFramework.Common;
using EyFramework.Library;
using SqlSugar;

namespace EyFramework.AutoCode
{
    public partial class FrmBuildAll : FrmDevBase
    {
        private AutoBuildService buildService = new AutoBuildService();

        public FrmBuildAll()
        {
            InitializeComponent();
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            Func<object> action = () => GenerateCode();
            bool result = bool.Parse(DoTaskAsync(action).ToString());
            if (result)
            {
                Globals.ShowTips("生成完毕");
            }
            else
            {
                Globals.ShowError("生成失败");
            }
        }

        private bool GenerateCode()
        {
            string connectName = cbDataBase.EditValue.ToString();
            string tableName = cbTables.EditValue.ToString();
            string moduleName = teModuleName.Text.Trim();
            string className = GetCamelString(tableName);
            string nameSpace = string.Empty;
            if (cbEntity.Checked)
            {
                nameSpace = moduleName + ".Entity";
                List<string> exclueFiles = new List<string>() { "CRE_USR_ID", "CRE_DT", "UPD_USR_ID", "UPD_DT" };
                if (buildService.BuildModel(PublicGlobal.DataBases, connectName, tableName, className, nameSpace,
                    beDirectory.Text.Trim(), exclueFiles))
                {
                    Console.WriteLine("Model生成成功");
                }
            }

            if (cbDao.Checked)
            {
                //生成Dao
                nameSpace = moduleName + ".Core.Dao";
                if (buildService.BuildDao(PublicGlobal.DataBases, connectName, tableName, className, moduleName, nameSpace, beDirectory.Text.Trim()))
                {
                    Console.WriteLine("Dao生成成功");
                }
            }

            if (cbIDao.Checked)
            {
                //生成IDao
                if (buildService.BuildIDao(PublicGlobal.DataBases, connectName, tableName, className, moduleName, nameSpace, beDirectory.Text.Trim()))
                {
                    Console.WriteLine("IDao生成成功");
                }
            }

            if (cbService.Checked)
            {
                //生成Service
                nameSpace = moduleName + ".Core.Service";
                if (buildService.BuildService(PublicGlobal.DataBases, connectName, tableName, className, moduleName, nameSpace, beDirectory.Text.Trim()))
                {
                    Console.WriteLine("Service生成成功");
                }
            }

            if (cbIService.Checked)
            {
                //生成ISservice
                if (buildService.BuildIService(PublicGlobal.DataBases, connectName, tableName, className, moduleName, nameSpace, beDirectory.Text.Trim()))
                {
                    Console.WriteLine("IService生成成功");
                }
            }

            return true;
        }

        private string GetCamelString(string tableName)
        {
            string sResult = string.Empty;
            string[] strArr = tableName.Split(new char[] { '_' });
            strArr.ForEach(p =>
            {
                if (!string.IsNullOrEmpty(p))
                {
                    sResult += p.Substring(0, 1).ToUpper() + (p.Length > 1 ? p.Substring(1).ToLower() : "");
                }
            });
            return sResult;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmBuildAll_Load(object sender, EventArgs e)
        {
            cbDataBase.Properties.Items.Clear();
            PublicGlobal.DataBases.OracleDbs.ForEach(p => { cbDataBase.Properties.Items.Add(p.ConnectName); });
            PublicGlobal.DataBases.SqlserverDbs.ForEach(p => { cbDataBase.Properties.Items.Add(p.ConnectName); });
        }

        private void cbDataBase_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<DbTableInfo> tables = buildService.queryAllTables(PublicGlobal.DataBases, cbDataBase.EditValue.ToString()).OrderBy(p => p.Name).ToList();
            tables.ForEach(p => { cbTables.Properties.Items.Add(p.Name); });
        }

        private void beDirectory_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (fbd.ShowDialog() == DialogResult.OK)
            {
                beDirectory.Text = fbd.SelectedPath;
            }
        }
    }
}